package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.LogInLogDao;
import com.kun.portal.webframe.po.FwUserPo;
import com.kun.portal.webframe.po.LoginLogPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lenovo on 2018/7/24.
 */
@Service
public class LoginLogService {
    @Autowired
    private LogInLogDao logInLogDao;
    @Autowired
    private EntityManager entityManager;

   @Transactional
    public Map<String,Object> getLoginInfo(FwUserPo userPo){
        Map result =  new HashMap<String,Object>();
         Query query = entityManager.createQuery("select l from LoginLogPo l left join fetch l.fwUserPo where l.fwUserPo.userId = :userId");
         query.setParameter("userId",userPo.getUserId());
         query.setMaxResults(10);
        List<LoginLogPo> list = query.getResultList();
        Query query1 = entityManager.createQuery("select count(l)as cou from LoginLogPo l where l.fwUserPo.userId= :userId2");
        query1.setParameter("userId2",userPo.getUserId());
        Object object =  query1.getSingleResult();
        result.put("count",object);
        result.put("log",list);
        return result;
    }
}
